<?php
namespace app\admin\controller;

use app\common\model\After as AfterModel;
use app\common\model\Category as CategoryModel;
use app\common\controller\AdminBase;
use think\Config;
use think\Db;

/**
 * 管理
 * Class Adminafter
 * @package app\admin\controller
 */
class After extends AdminBase
{
    protected $after_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->after_model = new AfterModel();
        $this->category_model = new CategoryModel();

        $category_level_list = $this->category_model->getLevelList();
        $this->assign('category_level_list', $category_level_list);

        $category = Db::name('category')->order('id DESC')->column('name','id');
        $article = Db::name('article')->where('status',1)->order('id DESC')->column('title','id');
        $user = Db::name('user')->where('status',1)->order('id DESC')->column('name','id');

        $this->assign('category',$category);
        $this->assign('article',$article);
        $this->assign('user',$user);

    }

    /**
     * 管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($category_id = 0,$user_name = '',$after_username = '', $page = 1)
    {
        $map = [];
        $tsgo = true;

        if ($user_name) {
            $user1 = Db::name('user')->where('status',1)->where('name',$user_name)->order('id DESC')->find();
            if($user1['id']){
                $map['user_id'] = ['like', "%{$user1['id']}%"];
            }else{
                $tsgo = false;
            }
            
        }

        if ($after_username) {
            $user2 = Db::name('user')->where('status',1)->where('name',$after_username)->order('id DESC')->find();
            if($user2['id']){
                $map['after_userid'] = ['like', "%{$user2['id']}%"];
            }else{
                $tsgo = false;
            }

        }

        if ($category_id > 0) {
            $category_children_ids = $this->category_model->where(['path' => ['like', "%,{$category_id},%"]])->column('id');
            $category_children_ids = (!empty($category_children_ids) && is_array($category_children_ids)) ? implode(',', $category_children_ids) . ',' . $category_id : $category_id;
            $map['category_id']            = ['IN', $category_children_ids];
        }

        if($tsgo){
            $after_list = $this->after_model->where($map)->order('id DESC')->paginate(15, false, ['page' => $page]);
        }else{
            $after_list = $this->after_model->where('id',0)->order('id DESC')->paginate(15, false, ['page' => $page]);
        }

        return $this->fetch('index', ['after_list' => $after_list, 'user_name' => $user_name,'after_username' => $after_username,'category_id' => $category_id]);
    }

    /**
     * 查看
     * @return mixed
     */
    public function check($id)
    {
        $after = $this->after_model->find($id);
        return $this->fetch('check', ['after' => $after]);
    }


    /**
     * 删除
     * @param $id
     */
    public function delete($id = '', $ids = [])
    {
        $id = $ids ? array_merge($ids) : $id;
        if ($id) {
            if ($this->after_model->destroy($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请选择需要删除的文章');
        }
    }
}